.PHONY: default_target check clippy depgraph gen_crate_inner_deps udeps \
dist_clean \
IT_local_mining IT_local_reorganize  UT_wallet_test_spend_utxo \
web_server web_server1 web_server2 \
mini_block_chain query_balance query_transaction send_transaction \
print_genesis_block \
run run_0 run_1
 

default_target:
	clear && cargo check && cargo build && cargo test

check:
	clear && cargo test && cargo run --bin print_genesis_block && RUST_BACKTRACE=1 cargo run --bin mini_block_chain

check-test:
	RUST_BACKTRACE=1 cargo run --bin mini_block_chain -- -d ./mini-bc-db-folder-new-0 -p 3001

clippy:
	cargo clippy --all-targets --all-features -- -D warnings

# cargo install cargo-depgraph
depgraph:
	cargo depgraph --workspace-only | dot -Tpng > ./gen/deps_in_workspace.png

gen_crate_inner_deps:
	./util_scripts/gen_crate_inner_mod_deps.sh

# cargo install cargo-udeps
# 安装 nightly 工具链 `rustup install nightly`
# 通过 +nightly 参数指定工具链
udeps:
	cargo +nightly udeps

dist_clean:
	rm -rf ./target/
	rm -rf ./gen/

IT_local_mining::
	./util_scripts/repeat_command.sh "cargo test --package tests --test local_mining -- local_mining --exact --show-output" 1000

IT_local_reorganize:
	./util_scripts/repeat_command.sh "cargo test --package tests --test local_reorganize_block -- reorganize_block --exact --show-output" 100

UT_wallet_test_spend_utxo:
	./util_scripts/repeat_command.sh "cargo test --package bc_wallet --lib -- wallet::tests::test_spend_utxo --exact --show-output" 100

# trace, debug, info, warn, error
# debug_level="info,bc_p2p=debug"
# debug_level=bc_p2p=debug
# debug_level=libp2p_mdns=trace
debug_level="debug,axum=debug"

web_server:
	RUST_LOG=${debug_level} cargo run --bin web_server

web_server1:
	RUST_LOG=${debug_level} cargo run --bin web_server1

web_server2:
	RUST_LOG=${debug_level} cargo run --bin web_server2

mini_block_chain:
	RUST_LOG=${debug_level} cargo run --bin mini_block_chain -- \
	-L debug
# --p2p-listen-address /ip4/127.0.0.1/tcp/40837 -L debug
# RUST_LOG=${debug_level} cargo run --bin mini_block_chain

run:
	cargo run --bin mini_block_chain

run_0:
	cargo run -r --bin mini_block_chain -- -p 4000 -d ./mini-bc-db-folder-new-0

run_1:
	cargo run -r --bin mini_block_chain -- -p 4001 -d ./mini-bc-db-folder-new-1


mini_block_chain-new-0:
	RUST_LOG=${debug_level} cargo run --bin mini_block_chain -- \
	-p 3001 \
	-d ./mini-bc-db-folder-new-0 -L debug
	# --p2p-listen-address /ip4/127.0.0.1/tcp/40838 -d ./mini-bc-db-folder-new-0 -L debug

mini_block_chain-new-1:
	RUST_LOG=${debug_level} cargo run --bin mini_block_chain -- \
	-p 3002 --p2p-listen-address /ip4/127.0.0.1/tcp/40839 -d ./mini-bc-db-folder-new-1 # -L info

mini_block_chain-new-2:
	RUST_LOG=${debug_level} cargo run --bin mini_block_chain -- \
	-p 3003 --p2p-listen-address /ip4/127.0.0.1/tcp/40840 -d ./mini-bc-db-folder-new-2 # -L info


bc-p2p:
	RUST_LOG=info cargo run --bin bc_p2p -- -L debug


print_genesis_block:
	RUST_LOG=${debug_level} cargo run --bin print_genesis_block
# 
# For test web server
# 
query_balance:
	curl -X POST http://localhost:3000/api/balance \
	     -H "Content-Type: application/json" \
	     -d '{"account": "9a3dd0eee41587a20d2a25be6a78d405e36a47aa"}'
	@echo ""

query_transaction:
	curl -X GET http://localhost:3000/api/transaction/9ddfd2ae61cbc5e74c0b1547c52a6363652233e33cd4d6527f7b65cbd7ef6c6f
	@echo ""

send_transaction:
	curl -X POST http://localhost:3000/api/transaction/send \
	-H "Content-Type: application/json" \
	-d '{"sender": "9a3dd0eee41587a20d2a25be6a78d405e36a47aa", "receiver": "f5bc9b2229b631d9c4dcf3cb82a58f30ac671a25", "amount": 3}'
# -d '{"sender": "12", "private_key": "34", "receiver": "56", "amount": 78}'
	@echo ""

start_mining:
	curl -X POST http://localhost:3000/api/mine \
		-H "Content-Type: application/json" \
		-d '{"account": "f0890fb76bbf77a1b8b7637ef8941dbb43f04232", "action": "start"}'

pause_mining:
	curl -X POST http://localhost:3000/api/mine \
		-H "Content-Type: application/json" \
		-d '{"account": "f0890fb76bbf77a1b8b7637ef8941dbb43f04232", "action": "pause"}'

stop_mining:
	curl -X POST http://localhost:3000/api/mine \
		-H "Content-Type: application/json" \
		-d '{"account": "f0890fb76bbf77a1b8b7637ef8941dbb43f04232", "action": "stop"}'

# 
# AccountManager:
# [keypair-0] 03721514a38bbe1cec0fab752beee25c62ac05228a66f230650b23ba30aafbb3e5 : 3654035c11efff36e9705543529d6509e744b93957e561dc69743add9f825488
# [keypair-1] 02830cb6c689754721e43a07ff8fbf890f4e0bddcff3eb5a33945eeed59c006c4b : 4b3df0b356987e0c7aa551f40c2557e5676dd6df240abc7809ac3454018f8abe
# [keypair-2] 034a7d3813d40c820a003341062e9a079adc5f568775a88088d5ee8d6d0ad0f7dd : ca618c44ef64121f0dc180711a300eb2040d182d50fc8c06c96006cfc9099d55
# [keypair-3] 03d993c8daee0b0f28e1b9e42af388d3433ccc87c9e2575404c6313d39bb0d65d3 : e106a5b17bef2e4d486c4a7dbd90d0b84f66b1eb7d0e8eaae221fdc7e2091b8e
# [keypair-4] 03f2f4dcf78e345485d30a645cd73b1fe4c388f7bea48fd4eaf45412e62d613999 : 6028c3c02cea18a873292e860860d6a97e057e346d3fa5a1ba96ed9ae6912e85
# [keypair-5] 02e6542a41ffd4fba43ff7b9cd437702333ac09af383fd3904eb280bc39e135004 : df0b10dc05117e1c874d1a9e9d8bed2bfda1ac639fad354c650d4fd9d202802e
## genesis ## [keypair-6] 029fb04b67ee96f862c622c4882d5940620ef7713d7903317a92d2cd3feb9ca106 : ad484ea9176c5e3541fb889f6cb5c5b7ed9bb4924cba353d57d6024d6403f72a
# [keypair-7] 027124431eed6cd11744652723c9d3e8b3726e67aff426153e259dd7fe6702df29 : e636b506e2b315ba2f4a3d1401229c276d9318300bd11111d5f3b5b8083a8d59
# [keypair-8] 034ff1e09915ec97fbac807109f6d0b9a67f18a9e215f5f62d54365dbae6bfeae8 : 0c734f26647e1ac61f3e51eeec0abc539eda0d3df19edd308bc02181c1c6609f
# [keypair-9] 0352148f0872b966f2f595ed57f8f8d76084219dc69642d86a74244954d111bc70 : b0c46fd52516ac59e2b3cb87b287e4e2441ce75c7c237aed532d469d4278b756
# [keypair-10] 03e3f9ae0e704d2cea207c50ae8e2a6195a4bc82cb4cd6e4079aee9a2b340a637b : 1e4edb6745d909b76917e81a216d2c40f6e4c345a7e649673cac5ca709d75887
# [keypair-11] 0240760f9caabedec5e8bb5a24bc0defe8a0a28a21e62eda55d7a6848eecfddf55 : 3e539abd441332e622e4bddbe8d2f2602b6147b779aedf7c7ed2f03a8c429893
# [keypair-12] 02326ca14e494ae872179b1c8fcdebd1a01cf3ef02e3df0822068d3eafafc61fb0 : d70088a5895166921ba8177c9226b7364e1ad605d25177f3e6ad2ba17ad41195
# [keypair-13] 02b97b3c115098f9f258b1fefe70bf5c50efbb051c8b62d5ea65094c043b5b3f34 : 79209d62d10d0c5e10895a187ecf8a3554f98fa0ace46c10eb9e537eec1a3dee
# [pubkeyhash-0] f0890fb76bbf77a1b8b7637ef8941dbb43f04232 : 02e6542a41ffd4fba43ff7b9cd437702333ac09af383fd3904eb280bc39e135004
# [pubkeyhash-1] f5bc9b2229b631d9c4dcf3cb82a58f30ac671a25 : 027124431eed6cd11744652723c9d3e8b3726e67aff426153e259dd7fe6702df29
# [pubkeyhash-2] f4bcd06e6c1db95147805e157d127f4cd2e80abf : 034a7d3813d40c820a003341062e9a079adc5f568775a88088d5ee8d6d0ad0f7dd
# [pubkeyhash-3] 025bf3f34d0688ea6929a985dc15023faadceb34 : 03e3f9ae0e704d2cea207c50ae8e2a6195a4bc82cb4cd6e4079aee9a2b340a637b
# [pubkeyhash-4] f96249fe9acc612920bb91b42d6c0eddf4b27170 : 02b97b3c115098f9f258b1fefe70bf5c50efbb051c8b62d5ea65094c043b5b3f34
# [pubkeyhash-5] 464ba1a1897ed851488dbce0cd0efaf440b8c8fa : 02830cb6c689754721e43a07ff8fbf890f4e0bddcff3eb5a33945eeed59c006c4b
# [pubkeyhash-6] ad37944b43baafa12421ab30f87453ccfb1399f3 : 034ff1e09915ec97fbac807109f6d0b9a67f18a9e215f5f62d54365dbae6bfeae8
# [pubkeyhash-7] e3d366474ded555cd6a6e735d09f94f6589ec235 : 0352148f0872b966f2f595ed57f8f8d76084219dc69642d86a74244954d111bc70
# [pubkeyhash-8] d5f4c8194bf765972012881b58fa9e72f6b0542c : 0240760f9caabedec5e8bb5a24bc0defe8a0a28a21e62eda55d7a6848eecfddf55
# [pubkeyhash-9] 7867ffcc47b5bb83d4bdbd04c7980b3b6bc59556 : 02326ca14e494ae872179b1c8fcdebd1a01cf3ef02e3df0822068d3eafafc61fb0
# [pubkeyhash-10] c0b86b8c786e9dc809f1045fba1a099900721568 : 03721514a38bbe1cec0fab752beee25c62ac05228a66f230650b23ba30aafbb3e5
# [pubkeyhash-11] a3ceb99d1bee293403f8be00d204809b0b20aebe : 03d993c8daee0b0f28e1b9e42af388d3433ccc87c9e2575404c6313d39bb0d65d3
# [pubkeyhash-12] 1ef558c4872cbb74a632a747b917a6587cb747df : 03f2f4dcf78e345485d30a645cd73b1fe4c388f7bea48fd4eaf45412e62d613999
## genesis ## [pubkeyhash-13] 9a3dd0eee41587a20d2a25be6a78d405e36a47aa : 029fb04b67ee96f862c622c4882d5940620ef7713d7903317a92d2cd3feb9ca106
# 
